/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;


import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.KeyNamePairs;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.MappingService;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceContentConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @Author Kent
 */
public class ImportRemoteServiceBusBindingContent extends Content {
	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;
	
	private VerticalPanel panel = null;
	
	private ListBox bus;
	
	
	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code1 = res.getCode();
			if (!code1.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			//Service s = (Service)context.get(ServiceConstants.SERVICE);
			//s.setId((String)res.getResponse());
			
			ArrayList<KeyNamePairs> l = (ArrayList<KeyNamePairs>)res.getResponse();
			bus.clear();
			bus.setVisibleItemCount(l.size() == 0 ? 1 : l.size() );
			for(KeyNamePairs k : l){
				bus.addItem(k.getName(), k.getId() );
			}

			
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};
	

	private AsyncCallback<ResponseObject> cb1 = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			MappingService s = (MappingService)context.get(ServiceConstants.MAPPING_SERVICE);
			//s.setId((String)res.getResponse());
			context.remove(ServiceConstants.MAPPING_SERVICE);
			context.remove(ServiceConstants.MAPPING_SERVICE_QOS);
			context.remove(ServiceConstants.BUSES);
			HashMap p = new HashMap();
			p.put("id", (String)res.getResponse());
			
			listener.onContentChanged(ServiceContentConstants.IMPORT_REMOTE_SERVICE_BUS_BINDING,
					ServiceContentConstants.REMOTE_SERVICE_ROOT,p );
			

			
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	
	public ImportRemoteServiceBusBindingContent(ServiceMessages cons,BifangGWTServiceAsync s,GwtContext c){
		this.service = s;
		this.cons = cons;
		this.context = c;
		
		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");
		
		initWidget(panel);
		
	}
	
	public void onLoad(){
		HTML h = new HTML(cons.bindingBus());
		panel.add(h);
		bus = new ListBox(true);
		bus.setWidth("11em");
		
		
		
		panel.add(bus);
		panel.add(new HTML("&nbsp;"));
		
		Button b = new Button(cons.previous());
		b.addClickListener(new ClickListener(){
			public void onClick(Widget send){
//				if(bus.getSelectedIndex() > 0){
//					context.put(ServiceConstants.BUS_ID, bus.getValue(bus.getSelectedIndex()));
//				}
				listener.onContentChanged(ServiceContentConstants.IMPORT_REMOTE_SERVICE_BUS_BINDING,
						ServiceContentConstants.IMPORT_REMOTE_SERVICE_CONFIG, null);
			}
			
		});
		
		Button b1 = new Button(cons.create());
		b1.addClickListener(new ClickListener(){
			public void onClick(Widget send){
				if(! validate()){
					return;
				}
				
				ArrayList s = new ArrayList();
				MappingService ser = (MappingService)context.get(ServiceConstants.MAPPING_SERVICE);
				
				
				s.add(ser);
				s.add(context.get(ServiceConstants.MAPPING_SERVICE_QOS));
				
				ArrayList<String> busR = new ArrayList<String>();
				for(int i = 0,j = bus.getItemCount(); i < j ; i ++){
					if(bus.isItemSelected(i)){
						busR.add(bus.getValue(i));
					}
				}
				s.add(busR);
				RequestObject req = new RequestObject();
				req.setAction("han.bifang.gwt.server.action.service.CreateMappingServiceAction");
				req.setRequest(s);
				service.call(req, cb1);
			}
			
		});
		
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(b);
		hp.add(new HTML("&nbsp;"));
		hp.add(b1);
		
		panel.add(hp);
		RequestObject req = new RequestObject();
		req.setAction("han.bifang.gwt.server.action.service.GetDomainBusListAction");
		MappingService s = (MappingService)context.get(ServiceConstants.MAPPING_SERVICE);
		
		req.setRequest(s.getDomainId());
		service.call(req, cb);
		
		
		
	}
	
	private boolean validate(){
		if (bus.getSelectedIndex() < 0) {
			Window.alert(cons.mustChooseBusError());
			return false;
		}
		return true;
		
	}

}
